package haierhome.krund.cn.daggertworetrofitrxjavamvp.retrofit;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import haierhome.krund.cn.daggertworetrofitrxjavamvp.R;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.dagger.MainActivity;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.retrofit.bean.ListData;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.retrofit.bean.NavigationData;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.retrofit.bean.SuceesContent;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.retrofit.bean.UserLogin;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.retrofit.net.NetConfig;
import haierhome.krund.cn.daggertworetrofitrxjavamvp.retrofit.net.NetInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitActivity extends AppCompatActivity {

    private Button bt_retrofit_get_noparam,
            bt_retrofit_get_param,
            bt_retrofit_post,
            bt_retrofit_post_map,
            bt_retrofit_post_file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);

        initView();
        initListener();
    }

    private void initListener() {
        //get请求无参数
        bt_retrofit_get_noparam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Retrofit retrofit = new Retrofit.Builder().
                        //基地址
                                baseUrl(NetConfig.BASE_PAHT).
                        //添加 转换工厂
                                addConverterFactory(GsonConverterFactory.create()).build();

                //网络请求方法
                NetInterface netInterface = retrofit.create(NetInterface.class);
                //进行网络请求
                netInterface.getRequest().enqueue(new Callback<NavigationData>() {
                    @Override
                    public void onResponse(Call<NavigationData> call, Response<NavigationData> response) {
                        if (response.body().getErrorCode() == 0) {
                            Toast.makeText(RetrofitActivity.this, response.body().getData().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<NavigationData> call, Throwable t) {

                    }
                });
            }
        });
        //get带网络请求参数
        bt_retrofit_get_param.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Retrofit retrofit = new Retrofit.Builder().
                        baseUrl(NetConfig.BASE_PAHT).
                        addConverterFactory(GsonConverterFactory.create()).build();

                NetInterface netInterface = retrofit.create(NetInterface.class);
                netInterface.getRequest("json", 2).enqueue(new Callback<ListData>() {
                    @Override
                    public void onResponse(Call<ListData> call, Response<ListData> response) {
                        if (response.body().getErrorCode() == 0) {
                            Toast.makeText(RetrofitActivity.this, response.body().getData().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ListData> call, Throwable t) {

                    }
                });
            }
        });
        //post请求第一种方式
        bt_retrofit_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofitPost = new Retrofit.Builder().
                        baseUrl(NetConfig.BASE_PAHT).
                        addConverterFactory(GsonConverterFactory.create()).build();

                NetInterface netInterfacePost = retrofitPost.create(NetInterface.class);
                netInterfacePost.postRequest1("545056@qq.com", "Spl901006ms").enqueue(new Callback<UserLogin>() {
                    @Override
                    public void onResponse(Call<UserLogin> call, Response<UserLogin> response) {
                        if (response.body().getErrorCode() == 0) {
                            Toast.makeText(RetrofitActivity.this, response.body().getData().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<UserLogin> call, Throwable t) {

                    }
                });
            }
        });
        //post第二种请求方式map
        bt_retrofit_post_map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofitPost = new Retrofit.Builder().
                        baseUrl(NetConfig.BASE_PAHT).
                        addConverterFactory(GsonConverterFactory.create()).build();

                NetInterface netInterfacePost = retrofitPost.create(NetInterface.class);
                //创建提交请求参数集体
                Map<String, String> map = new HashMap<>();
                map.put("username", "545056@qq.com");
                map.put("password", "Spl901006ms");
                //map方式进行网络请求
                netInterfacePost.postRequestMap(map).enqueue(new Callback<UserLogin>() {
                    @Override
                    public void onResponse(Call<UserLogin> call, Response<UserLogin> response) {
                        if (response.body().getErrorCode() == 0) {
                            Toast.makeText(RetrofitActivity.this, response.body().getData().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<UserLogin> call, Throwable t) {

                    }
                });
            }
        });
        //post第三种请求方式上传文件  这个接口有问题
        bt_retrofit_post_file.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofitpost3 = new Retrofit.Builder().baseUrl(NetConfig.BASE_PAHT).addConverterFactory(GsonConverterFactory.create()).build();
                NetInterface netInterface3 = retrofitpost3.create(NetInterface.class);
                String filePath = Environment.getExternalStorageDirectory()+ File.separator+"xUtils.db";
                netInterface3.postFile(new File(filePath)).enqueue(new Callback<SuceesContent>() {
                    @Override
                    public void onResponse(Call<SuceesContent> call, Response<SuceesContent> response) {
                        if (response.body().getSuccess()!=null){
                            Toast.makeText(RetrofitActivity.this,response.body().getSuccess().toString(),Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<SuceesContent> call, Throwable t) {

                    }
                });
            }
        });
    }

    private void initView() {
        bt_retrofit_get_noparam = (Button) findViewById(R.id.bt_retrofit_get_noparam);
        bt_retrofit_get_param = (Button) findViewById(R.id.bt_retrofit_get_param);
        bt_retrofit_post = (Button) findViewById(R.id.bt_retrofit_post);
        bt_retrofit_post_map = (Button) findViewById(R.id.bt_retrofit_post_map);
        bt_retrofit_post_file = (Button) findViewById(R.id.bt_retrofit_post_file);
    }
}
